package com.coai.message.controller

import com.coai.common.dto.ApiResponse
import com.coai.message.dto.*
import com.coai.message.service.MessageService
import jakarta.validation.Valid
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*
import java.time.LocalDateTime

@RestController
@RequestMapping("/api/messages")
class MessageController(
    private val messageService: MessageService
) {
    
    @PostMapping
    fun sendMessage(
        @Valid @RequestBody request: SendMessageRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageDto> {
        val message = messageService.sendMessage(request, userId)
        return ApiResponse.success(message, "发送成功")
    }
    
    @GetMapping("/conversation/{conversationId}")
    fun getMessages(
        @PathVariable conversationId: String,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) beforeTime: LocalDateTime?,
        @RequestParam(defaultValue = "20") limit: Int,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageListResponse> {
        val messages = messageService.getMessages(conversationId, userId, beforeTime, limit)
        return ApiResponse.success(messages)
    }
    
    @PostMapping("/read")
    fun markMessagesAsRead(
        @RequestParam conversationId: String,
        @Valid @RequestBody request: MarkAsReadRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Unit> {
        messageService.markMessagesAsRead(conversationId, request.messageIds, userId)
        return ApiResponse.success(Unit, "已标记为已读")
    }
    
    @PostMapping("/conversation/{conversationId}/read-all")
    fun markConversationAsRead(
        @PathVariable conversationId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Unit> {
        messageService.markConversationAsRead(conversationId, userId)
        return ApiResponse.success(Unit, "已全部标记为已读")
    }
    
    @DeleteMapping("/{messageId}/recall")
    fun recallMessage(
        @PathVariable messageId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<MessageDto> {
        val message = messageService.recallMessage(messageId, userId)
        return ApiResponse.success(message, "消息已撤回")
    }
    
    @GetMapping("/search")
    fun searchMessages(
        @RequestParam conversationId: String,
        @RequestParam keyword: String,
        @RequestParam(defaultValue = "50") limit: Int,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<List<MessageDto>> {
        val messages = messageService.searchMessages(conversationId, keyword, userId, limit)
        return ApiResponse.success(messages)
    }
    
    @GetMapping("/conversation/{conversationId}/unread-count")
    fun getUnreadCount(
        @PathVariable conversationId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<Long> {
        val count = messageService.getUnreadCount(conversationId, userId)
        return ApiResponse.success(count)
    }
}
